package cn.gemframe.boot;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.boot.autoconfigure.SpringBootApplication;

public class GemFrameApplicationRun {

	public static void customScan(Object source) {
		try {
			String SCN_NAME = "cn.gemframe";
			Class clazz = (Class) source;
			String name = clazz.getName();
			name = name.substring(0, name.lastIndexOf("."));
			SpringBootApplication annotation = (SpringBootApplication) clazz.getAnnotation(SpringBootApplication.class);
			if (annotation != null) {
				InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
				Field value = invocationHandler.getClass().getDeclaredField("memberValues");
				value.setAccessible(true);
				Map<String, Object> memberValues = (Map<String, Object>) value.get(invocationHandler);
				String[] basePackages = (String[]) memberValues.get("scanBasePackages");
				List<String> arrayList = new ArrayList<String>();
				arrayList.add(SCN_NAME);
				if (basePackages != null && basePackages.length > 0) {
					for (String scn : basePackages) {
						arrayList.add(scn);
					}
				} else {
					if (!SCN_NAME.contains(name)) {
						arrayList.add(name);
					}
				}
				memberValues.put("scanBasePackages", arrayList.toArray(new String[arrayList.size()]));
			}
		} catch (Exception e) {

		}
	}

}
